﻿using RpcHelper;
using SqlExecHelper;
using SqlExecHelper.SetColumn;
using SqlExecHelper.SqlValue;

using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
        public class ServerRegionDAL : SqlBasicClass
        {
                public ServerRegionDAL() : base("ServerRegion")
                {

                }
                public bool CheckName(string name)
                {
                        return base.CheckIsExists(new ISqlWhere[] {
                                new SqlWhere("RegionName", System.Data.SqlDbType.NVarChar,50){Value=name},
                                new SqlWhere("IsDrop", System.Data.SqlDbType.Bit){Value=0}
                        });
                }

                public ServerRegion[] Gets ()
                {
                        return base.Get<bool, ServerRegion> ("IsDrop", false);
                }
                public ServerRegion[] Gets (int[] ids)
                {
                        return base.Get<int, ServerRegion> ("Id", ids);
                }
                public int Add(string name)
                {
                        base.Insert(new IInsertSqlValue[] {
                                new InsertSqlValue("RegionName", System.Data.SqlDbType.NVarChar,50){ Value=name}
                        }, out int id);
                        return id;
                }
                public void Set(int id, string name)
                {
                        if(!this.Update(new ISqlSetColumn[] {
                                new SqlSetColumn("RegionName",  System.Data.SqlDbType.NVarChar,50){Value=name}
                        }, "Id", id))
                        {
                                throw new ErrorException ("rpc.region.set.error");
                        }
                }
                public void Drop(int id)
                {
                        if(!this.Update(new ISqlSetColumn[] {
                                new SqlSetColumn("IsDrop",  System.Data.SqlDbType.Bit){Value=1}
                        }, "Id", id))
                        {
                                throw new ErrorException ("rpc.region.drop.error");
                        }
                }
        }
}
